import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { PageQuery, PageResult } from '@/types/common';

/**
 * 还款计划查询对象
 */
export interface RepaymentQuery extends PageQuery {
  orderId?: number;
  orderNo?: string;
  periodNo?: number;
  repaymentStatus?: string;
  beginRepaymentDate?: string;
  endRepaymentDate?: string;
  overdue?: boolean;
}

/**
 * 还款计划业务对象
 */
export interface RepaymentForm {
  planId?: number;
  orderId: number;
  periodNo?: number;
  repaymentAmount?: number;
  principal?: number;
  interest?: number;
  repaymentDate?: string;
  actualRepaymentTime?: string;
  repaymentStatus?: string;
  overdueDays?: number;
  overdueAmount?: number;
}

/**
 * 还款计划视图对象
 */
export interface RepaymentVO {
  planId: number;
  orderId: number;
  periodNo: number;
  repaymentAmount: number;
  principal: number;
  interest: number;
  repaymentDate: string;
  actualRepaymentTime: string;
  repaymentStatus: string;
  overdueDays: number;
  overdueAmount: number;
  createTime: string;
}

/**
 * 手动扣款请求对象
 */
export interface ManualDeductRequest {
  planId: number;
}

/**
 * 催收请求对象
 */
export interface RemindRequest {
  planId: number;
  remindMessage?: string;
}

/**
 * 减免逾期费请求对象
 */
export interface WaiveOverdueFeeRequest {
  planId: number;
  waiveAmount?: number;
  waiveReason?: string;
}

/**
 * 查询还款计划列表
 */
export function listRepayment(query: RepaymentQuery): AxiosPromise<PageResult<RepaymentVO>> {
  return request({
    url: '/admin/beauty/repayment/list',
    method: 'get',
    params: query
  });
}

/**
 * 查询还款计划详细
 */
export function getRepayment(planId: number): AxiosPromise<RepaymentVO> {
  return request({
    url: '/admin/beauty/repayment/' + planId,
    method: 'get'
  });
}

/**
 * 根据订单ID查询还款计划
 */
export function getRepaymentByOrderId(orderId: number): AxiosPromise<RepaymentVO[]> {
  return request({
    url: '/admin/beauty/repayment/order/' + orderId,
    method: 'get'
  });
}

/**
 * 新增还款计划
 */
export function addRepayment(data: RepaymentForm): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment',
    method: 'post',
    data: data
  });
}

/**
 * 修改还款计划
 */
export function updateRepayment(data: RepaymentForm): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment',
    method: 'put',
    data: data
  });
}

/**
 * 删除还款计划
 */
export function delRepayment(planIds: number | number[]): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/' + planIds,
    method: 'delete'
  });
}

/**
 * 手动扣款
 */
export function manualDeduct(data: ManualDeductRequest): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/manualDeduct',
    method: 'post',
    data: data
  });
}

/**
 * 批量手动扣款
 */
export function batchManualDeduct(planIds: number[]): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/batchManualDeduct',
    method: 'post',
    data: planIds
  });
}

/**
 * 催收
 */
export function remindRepayment(data: RemindRequest): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/remind',
    method: 'post',
    data: data
  });
}

/**
 * 减免逾期费
 */
export function waiveOverdueFee(data: WaiveOverdueFeeRequest): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/waiveOverdueFee',
    method: 'post',
    data: data
  });
}

/**
 * 生成还款计划
 */
export function generateRepaymentPlan(orderId: number): AxiosPromise<void> {
  return request({
    url: '/admin/beauty/repayment/generate/' + orderId,
    method: 'post'
  });
}

/**
 * 导出还款计划
 */
export function exportRepayment(query: RepaymentQuery): AxiosPromise<Blob> {
  return request({
    url: '/admin/beauty/repayment/export',
    method: 'post',
    params: query,
    responseType: 'blob'
  });
}
